package com.aia.ilp.web;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import net.coobird.thumbnailator.Thumbnails;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.aia.ilp.domain.Image;
import com.aia.ilp.domain.Thumb;
import com.aia.ilp.service.MainService;
import com.cfh.util.Guid;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport implements SessionAware,
		ServletRequestAware, ServletContextAware {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(UploadAction.class);

	private static final long serialVersionUID = 1L;

	private Map session;
	private File image;
	private String imageFileName;
	private String dream;
	private String mail;
	HttpServletRequest servletRequest;
	ServletContext servletContext;

	private MainService mainService;

	public void setSession(Map map) {
		session = map;
	}
	public String execute() {
		String guid = null;
		String ContentRootPath = null;
		try {
			if (logger.isDebugEnabled()) {
				logger.debug("execute()"); //$NON-NLS-1$
			}
			{// Validate

			}
			{// Get GUID
				Guid guidGen = new Guid();
				guid = guidGen.toString();

			}
			{// save original image
				logger.debug("execute()"); //$NON-NLS-1$

				ContentRootPath = servletContext.getRealPath("/");

				logger.debug("execute()"); //$NON-NLS-1$
				logger.debug("execute()"); //$NON-NLS-1$
				logger.debug("execute()"); //$NON-NLS-1$
				File fileToCreate = new File(ContentRootPath + "/upload",
						this.imageFileName);

				Image record = new Image();
				record.setIMG_GUID(guid);
				byte[] IMG_BIN = null;
				try {
					IMG_BIN = IOUtils.toByteArray(new FileInputStream(image));
				} catch (FileNotFoundException e1) {
					logger.error("execute()", e1); //$NON-NLS-1$
					throw new RuntimeException(e1);
				} catch (IOException e1) {
					logger.error("execute()", e1); //$NON-NLS-1$
					throw new RuntimeException(e1);
				}
				record.setIMG_BIN(IMG_BIN);
				mainService.saveImage(record);

				try {
					FileUtils.copyFile(this.image, fileToCreate);
				} catch (IOException e) {
					logger.error("execute()", e); //$NON-NLS-1$
					throw new RuntimeException(e);
				}
				if (logger.isDebugEnabled()) {
					logger.debug("execute()"); //$NON-NLS-1$
				}
			}

			{// save thumb image
			// String fileName = ContentRootPath + "/uploadTemp/" + guid
			// + "_thumb.jpg";
				ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
				Thumbnails.of(image).size(120, 120).outputFormat("jpg")
						.outputQuality(0.6).toOutputStream(outputStream);
				byte[] thumbBytes = outputStream.toByteArray();
				Thumb record = new Thumb();
				record.setTHUMB_GUID(guid);
				record.setTHUMB_BIN(thumbBytes);
				mainService.saveThumb(record);
			}
		} catch (Exception e) {
			logger.error("execute()", e); //$NON-NLS-1$
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	public String execute2() {
		if (logger.isDebugEnabled()) {
			logger.debug("execute2() - start"); //$NON-NLS-1$
		}
		System.out.println("Dream=" + dream);

		String filePath = servletContext.getRealPath("/");

		logger.debug("execute2()"); //$NON-NLS-1$
		logger.debug("execute2()"); //$NON-NLS-1$
		logger.debug("execute2()"); //$NON-NLS-1$
		File fileToCreate = new File(filePath + "/upload", this.imageFileName);
		try {
			FileUtils.copyFile(this.image, fileToCreate);
		} catch (IOException e) {
			logger.error("execute2()", e); //$NON-NLS-1$
			throw new RuntimeException(e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("execute2() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public File getImage() {
		return image;
	}

	public void setServletRequest(HttpServletRequest request) {
		servletRequest = request;
	}

	public void setServletContext(ServletContext context) {
		servletContext = context;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setDream(String dream) {
		this.dream = dream;
	}

	public String getDream() {
		return dream;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getMail() {
		return mail;
	}

	public void setMainService(MainService mainService) {
		this.mainService = mainService;
	}

	public MainService getMainService() {
		return mainService;
	}

}
